/*
 *   Copyright 2012, Thomas Kerber
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package milk.jpatch.fileLevel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import milk.jpatch.DirectorySerializable;

/**
 * A patch of a file.
 * @author Thomas Kerber
 * @version 1.0.3
 */
public abstract class FilePatch implements DirectorySerializable{
    
    /**
     * The filename.
     */
    protected String name;
    
    /**
     * Whether or not to generate file removal patches.
     */
    // TODO: make this config/commandline/w/e
    public static final boolean GENERATE_FILE_REMS = false;
    
    /**
     * Creates. Constructor required for serializability.
     */
    protected FilePatch(){
        
    }
    
    /**
     * Creates.
     * @param name The filename.
     */
    protected FilePatch(String name){
        this.name = name;
    }
    
    /**
     * 
     * @return The filename.
     */
    public String getName(){
        return name;
    }
    
    /**
     * Patches a stream.
     * @param in The input stream.
     * @param out The output stream.
     * @throws IOException If an error occures reading/writing to the streams.
     */
    public abstract void patch(InputStream in, OutputStream out)
            throws IOException;
    
    /**
     * Patches a file.
     * @param from The file to patch.
     * @param to Where to output the result.
     * @throws IOException If an error occurs reading/writing to the files.
     */
    public void patch(File from, File to) throws IOException{
        FileInputStream in = null;
        try{
            in = new FileInputStream(from);
        }
        catch(FileNotFoundException e){
            // Squash. Really. This is to enable the replace to work *without*
            // a file on the other end.
        }
        
        patch(in, new FileOutputStream(to));
    }
    
    /**
     * Generates.
     * @param rootOrig The root of the original.
     * @param rootMod The root of the modded version.
     * @param name The files name in relation to the roots.
     * @return The file patch.
     * @throws IOException If a read error occurs.
     */
    public static FilePatch generate(File rootOrig, File rootMod, String name)
            throws IOException{
        File fOrig = new File(rootOrig, name);
        File fMod = new File(rootMod, name);
        
        if(!fOrig.isFile() && fMod.isFile())
            return new FileReplace(rootMod, name);
        else if(fOrig.isFile() && !fMod.isFile()){
            if(GENERATE_FILE_REMS)
                return new FileRemove(name);
            else
                return new FileID(name);
        }
        else if(fOrig.isFile() && fMod.isFile()){
            if(name.endsWith(".class"))
                return ClassPatch.generate(fOrig, fMod, name);
            else
                return new FileReplace(rootMod, name);
        }
        else{
            throw new IllegalArgumentException(
                    "Cannot generate FilePatch from no files.");
        }
    }
    
    /**
     * 
     * @param from The name of the file to patch.
     * @param to The name of the output file.
     * @throws IOException
     */
    public void patch(String from, String to) throws IOException{
        patch(new File(from), new File(to));
    }
    
    @Override
    public abstract void serialize(File dir) throws IOException;
    
}
